package com.stars.pao.springbook.service;

import com.stars.pao.springbook.enums.BookStatusEnums;
import com.stars.pao.springbook.mapper.BookInfoMapper;
import com.stars.pao.springbook.model.BookInfo;
import com.stars.pao.springbook.model.PageRequest;
import com.stars.pao.springbook.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {


    @Autowired
    private BookInfoMapper bookInfoMapper;


    public Integer insertBook(BookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
//        1. 查询数据
        Integer count = bookInfoMapper.count();
//        2. 查询当前页的数据
        List<BookInfo> bookInfos = bookInfoMapper.queryListBypage(pageRequest);

//        PageResult<BookInfo> pageResult = new PageResult<>();
//        pageResult.setRecords(bookInfos);
//        pageResult.setCount(count);
//        return pageResult;
//        for(BookInfo bookInfo: bookInfos) {
//            if(bookInfo.getStatus() == 1) {
//                bookInfo.setStateCN("可借阅");
//            } else if(bookInfo.getStatus() == 2){
//                bookInfo.setStateCN("不可借阅");
//            }else {
//                bookInfo.setStateCN("无效");
//            }
//        }
//        设置Status
        for(BookInfo bookInfo: bookInfos) {
            bookInfo.setStateCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos, count,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
